home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (c) 1993 Michael D. Bayne.
- * All rights reserved.
- *
- * Please see the documentation accompanying the distribution for distribution and disclaimer information.
- */
-
- #include <exec/types.h>
- #include <exec/memory.h>
-
- #include <dos/dos.h>
- #include <workbench/startup.h>
- #include <workbench/workbench.h>
-
- #include <libraries/iffparse.h>
- #include <libraries/reqtools.h>
-
- #include <clib/exec_protos.h>
- #include <clib/dos_protos.h>
- #include <clib/reqtools_protos.h>
- #include <clib/graphics_protos.h>
- #include <clib/iffparse_protos.h>
- #include <clib/icon_protos.h>
- #include <clib/wb_protos.h>
- #include <clib/utility_protos.h>
-
- #include <string.h>
- #include <stdlib.h>
-
- #include "defs.h"
- #include "Blanker.h"
-
- #define ID_PREF MAKE_ID('P','R','E','F')
- #define ID_PRHD MAKE_ID('P','R','H','D')
- #define ID_BLNK MAKE_ID('B','L','N','K')
-
- #define BPREFSIZE 524
-
- UWORD RangeRand( UWORD );
-
- #include "protos/main.h"
- #include "protos/winhand.h"
- #include "protos/messaging.h"
-
- UBYTE *doPath( UBYTE *file, UBYTE *dir )
- {
- static UBYTE path[256];
-
- CopyMem( dir, path, 108 );
- if( !AddPart( path, file, 256 )) return( 0L );
- return( path );
- }
-
- UBYTE *longToStr( LONG num )
- {
- static UBYTE out[32];
- LONG i = 32, j;
-
- while( num ) { out[--i] = num % 10; num /= 10; }
- for( j = 0; j < 32 - i; j++ ) out[j] = '0' + out[i+j];
- out[j] = 0;
-
- return( out );
- }
-
- VOID prefError( LONG rc )
- {
- const char *errMsgs[] = { "IFF handle allocation failed. (1)",
- "Open() (for read) failed on pref file. (2)",
- "OpenIFF() (for read) failed on pref file. (3)",
- "Internal error (Context Node retrieval failed). (4)",
- "No BLNK chunk found in pref file. (5)",
- "Open() (for write) failed on pref file. (6)",
- "OpenIFF() (for write) failed on pref file. (7)"
- };
-
- rtEZRequest( "Error processing preferences file:\n\n%s", "Ok", 0L, BTAGS, errMsgs[rc-1] );
- }
-
- LONG loadModulePrefs( UBYTE *path, struct BlankerPrefs *bPO )
- {
- struct IFFHandle *bHandle;
- struct ContextNode *top;
- LONG rc = -1, err;
-
- if(!( bHandle = AllocIFF())) return( 1 );
-
- if( bHandle->iff_Stream = (ULONG)Open( path, MODE_OLDFILE )) {
- InitIFFasDOS( bHandle );
-
- if( !OpenIFF( bHandle , IFFF_READ )) {
- while( rc == -1 ) {
- err = ParseIFF( bHandle, IFFPARSE_RAWSTEP );
- switch( err ) {
- case IFFERR_EOC:
- if( top = CurrentChunk( bHandle )) {
- if(( top->cn_Type == ID_PREF )&&( top->cn_ID == ID_BLNK )) {
- ReadChunkBytes( bHandle, &(bPO->bp_Mode), BPREFSIZE );
- rc = 0;
- }
- } else rc = 4;
- break;
- case IFFERR_EOF:
- rc = 5;
- default:
- break;
- }
- }
- CloseIFF( bHandle );
- } else rc = 3;
- Close( bHandle->iff_Stream );
- } else rc = 2;
- FreeIFF( bHandle );
-
- return( rc );
- }
-
- LONG saveModulePrefs( UBYTE *path, struct BlankerPrefs *bPO )
- {
- struct IFFHandle *bHandle;
- UBYTE prefHead[] = {0,0,0,0,0,0};
- LONG rc = 0;
-
- if(!( bHandle = AllocIFF())) return( 1 );
-
- if( bHandle->iff_Stream = (ULONG)Open( path, MODE_NEWFILE )) {
- InitIFFasDOS( bHandle );
-
- if( !OpenIFF( bHandle , IFFF_WRITE )) {
- PushChunk( bHandle, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN );
-
- PushChunk( bHandle, ID_PREF, ID_PRHD, 6 );
- WriteChunkBytes( bHandle, prefHead, 6 );
- PopChunk( bHandle );
-
- PushChunk( bHandle, ID_PREF, ID_BLNK, BPREFSIZE );
- WriteChunkBytes( bHandle, &(bPO->bp_Mode), BPREFSIZE );
- PopChunk( bHandle );
-
- PopChunk( bHandle );
- CloseIFF( bHandle );
- } else rc = 7;
- Close( bHandle->iff_Stream );
- } else rc = 6;
- FreeIFF( bHandle );
-
- return( rc );
- }
-
- VOID readBlankerToolTypes( UBYTE *path, struct BlankerPrefs *bPO )
- {
- struct DiskObject *bDO;
- UBYTE *tooltype;
-
- if( bDO = GetDiskObject( path )) {
- if( tooltype = FindToolType( bDO->do_ToolTypes, "CX_PRIORITY" ))
- bPO->bp_Priority = atoi( tooltype );
- if( tooltype = FindToolType( bDO->do_ToolTypes, "CX_POPUP" ))
- bPO->bp_PopUp = (ULONG)MatchToolValue( tooltype, "YES" );
- if( tooltype = FindToolType( bDO->do_ToolTypes, "CX_POPKEY" ))
- CopyMem( tooltype, bPO->bp_PopKey, 128 );
- if( tooltype = FindToolType( bDO->do_ToolTypes, "BLANKKEY" ))
- CopyMem( tooltype, bPO->bp_BlankKey, 128 );
- if( tooltype = FindToolType( bDO->do_ToolTypes, "TIMEOUT" ))
- bPO->bp_Timeout = 10*atoi( tooltype );
- if( tooltype = FindToolType( bDO->do_ToolTypes, "RANDTIMEOUT" ))
- bPO->bp_RandTimeout = atoi( tooltype );
- if( tooltype = FindToolType( bDO->do_ToolTypes, "BLANKERDIR" ))
- CopyMem( tooltype, bPO->bp_Dir, 128 );
- if( tooltype = FindToolType( bDO->do_ToolTypes, "BLANKER" ))
- CopyMem( tooltype, bPO->bp_Name, 128 );
- FreeDiskObject( bDO );
- }
-
- if( !bPO->bp_Timeout ) bPO->bp_Timeout = 1800;
- }
-
- VOID writeBlankerToolTypes( UBYTE *path, struct BlankerPrefs *bPO, ULONG main )
- {
- #define NUMTOOLTYPES 9
- struct DiskObject *bDO;
- UBYTE **oldToolTypes, *oldDefTool, *toolTypes[NUMTOOLTYPES+1], *defToolTypes[] =
- { "DONOTWAIT", "CX_PRIORITY=", "CX_POPUP=", "CX_POPKEY=", "BLANKKEY=","TIMEOUT=",
- "RANDTIMEOUT=", "BLANKERDIR=", "BLANKER=" };
- LONG i;
-
- for( i = 0; i < NUMTOOLTYPES; i++ )
- if( toolTypes[i] = AllocVec( 160, MEMF_CLEAR )) strcpy( toolTypes[i], defToolTypes[i] );
- else break;
- if( i < NUMTOOLTYPES ) while( --i > -1 ) FreeVec( toolTypes[i] );
- else {
- strcat( toolTypes[1], longToStr( bPO->bp_Priority ));
- strcat( toolTypes[2], bPO->bp_PopUp ? "YES" : "NO" );
- strcat( toolTypes[3], bPO->bp_PopKey );
- strcat( toolTypes[4], bPO->bp_BlankKey );
- strcat( toolTypes[5], longToStr( bPO->bp_Timeout/10 ));
- strcat( toolTypes[6], longToStr( bPO->bp_RandTimeout ));
- strcat( toolTypes[7], bPO->bp_Dir );
- strcat( toolTypes[8], bPO->bp_Name );
- toolTypes[NUMTOOLTYPES] = NULL;
-
- if(!( bDO = GetDiskObject( path ))) bDO = GetDiskObjectNew( "ENVARC:sys/def_pref" );
-
- if( bDO ) {
- oldToolTypes = bDO->do_ToolTypes;
- if( main ) bDO->do_ToolTypes = toolTypes;
- else bDO->do_ToolTypes = &(toolTypes[6]);
-
- oldDefTool = bDO->do_DefaultTool;
- bDO->do_DefaultTool = "Blanker";
-
- PutDiskObject( path, bDO );
-
- bDO->do_ToolTypes = oldToolTypes;
- bDO->do_DefaultTool = oldDefTool;
-
- FreeDiskObject( bDO );
- }
-
- for( i = 0; i < NUMTOOLTYPES; i++ ) FreeVec( toolTypes[i] );
- }
- }
-
- LONG loadBlankerPrefs( UBYTE *file, UBYTE *dir, struct BlankerPrefs *bP )
- {
- LONG rc;
-
- readBlankerToolTypes( doPath( file, dir ), bP );
- rc = loadModulePrefs( doPath( file, dir ), bP );
- loadModule( 0 );
- if( BlankerWnd ) setPrefs( bP, Blanker_CNT );
-
- return( rc );
- }
-
- VOID saveBlankerPrefs( UBYTE *file, UBYTE *dir, struct BlankerPrefs *bPO )
- {
- writeBlankerToolTypes( doPath( file, dir ), bPO, 0L );
- saveModulePrefs( doPath( file, dir ), bPO );
- }
-
- VOID saveDefaultPrefs( struct BlankerPrefs *bPO )
- {
- writeBlankerToolTypes( "PROGDIR:Blanker", bPO, 1L );
- saveModulePrefs( "ENV:Blanker.prefs", bPO );
- saveModulePrefs( "ENVARC:Blanker.prefs", bPO );
- }
-
- LONG loadPrefsFromWBArgList( struct WBArg *WBArgs, ULONG numArgs, struct BlankerPrefs *bPO )
- {
- LONG i, rc = 0L;
- extern UBYTE prefDir[];
-
- for( i = 0; i < numArgs; i++, WBArgs++ )
- if( WBArgs->wa_Name && NameFromLock( WBArgs->wa_Lock, prefDir, 108 ))
- if(!( rc = loadBlankerPrefs( WBArgs->wa_Name, prefDir, bPO ))) break;
- return( rc );
- }
-
- LONG loadPrefsFromENVARC( struct BlankerPrefs *bPO )
- {
- LONG rc;
-
- readBlankerToolTypes( "PROGDIR:Blanker", bPO );
- rc = loadModulePrefs( "ENV:Blanker.prefs", bPO );
- loadModule( 0 );
- if( BlankerWnd ) setPrefs( bPO, Blanker_CNT );
-
- return( rc );
- }
-
- VOID FreeRPList( struct RandomPrefsList *rPL )
- {
- struct RandomPrefs *Temp;
-
- rPL->rpl_Date.ds_Days = 0L;
- rPL->rpl_Date.ds_Minute = 0L;
- rPL->rpl_Date.ds_Tick = 0L;
- rPL->rpl_Number = 0;
- while( rPL->rpl_Head ) {
- Temp = rPL->rpl_Head;
- rPL->rpl_Head = Temp->rp_Next;
- FreeMem( Temp, sizeof( struct RandomPrefs ));
- }
- }
-
- VOID buildRandList( struct RandomPrefsList *rPrefs, UBYTE *dir )
- {
- struct FileInfoBlock *Blk;
- struct RandomPrefs *Current;
- BPTR lock;
- LONG len;
-
- if(( Blk = AllocDosObject( DOS_FIB, 0L ))&&( lock = Lock( dir, ACCESS_READ ))&&( Examine( lock, Blk ))) {
- if( CompareDates( &rPrefs->rpl_Date, &Blk->fib_Date ) > 0 ) {
- #ifdef STDIO
- printf( "Rand Pref list is out of date. Rebuilding.\n" );
- #endif
- FreeRPList( rPrefs );
- CopyMem( &Blk->fib_Date, &rPrefs->rpl_Date, sizeof( struct DateStamp ));
- while( ExNext( lock, Blk )) {
- if( Blk->fib_DirEntryType < 0 ) {
- len = strlen( Blk->fib_FileName );
- if( Stricmp( &(Blk->fib_FileName[len-5]), ".info" )&&
- ( Current = AllocMem( sizeof( struct RandomPrefs ), MEMF_CLEAR ))) {
- #ifdef STDIO
- printf( "Adding %s to list.\n", Blk->fib_FileName );
- #endif
- CopyMem( Blk->fib_FileName, Current->rp_Name, 108 );
- rPrefs->rpl_Number += 1;
- Current->rp_Next = rPrefs->rpl_Head;
- rPrefs->rpl_Head = Current;
- }
- #ifdef STDIO
- else printf( "Skipping %s.\n", Blk->fib_FileName );
- #endif
- }
- }
- if( IoErr() != ERROR_NO_MORE_ENTRIES ) rPrefs->rpl_Number = 0;
- }
- #ifdef STDIO
- else printf( "Rand Pref list is up to date.\n" );
- #endif
- }
- if( Blk ) FreeDosObject( DOS_FIB, Blk );
- if( !rPrefs->rpl_Number ) FreeRPList( rPrefs );
- }
-
- VOID loadRandPrefs( struct RandomPrefsList *rPL, struct BlankerPrefs *bPO )
- {
- UWORD i, num;
- struct RandomPrefs *Current;
- UBYTE *dir = "SYS:Prefs/Presets/Blanker";
-
- buildRandList( rPL, dir );
-
- if( rPL->rpl_Number ) {
- num = RangeRand(( UWORD )rPL->rpl_Number );
- for( i = 0, Current = rPL->rpl_Head; i < num; ++i, Current = Current->rp_Next );
- #ifdef STDIO
- printf( "Chose %d. Loading %s from %s.\n", num, Current->rp_Name, dir );
- #endif
- loadBlankerPrefs( Current->rp_Name, dir, bPO );
- }
- }
-